package com.powernobug.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.KeywordListParam;
import com.powernobug.service.KeywordService;
import com.powernobug.service.impl.KeywordServiceImpl;
import com.powernobug.util.JacksonUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 *
 */
/*

 */
@WebServlet("/keyword/*")
public class AdminKeywordServlet extends CommonServlet{
    KeywordService keywordService = new KeywordServiceImpl();

    public BaseRespVo list(HttpServletRequest request) throws IOException, ParseException {
        // 1、接收请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        KeywordListParam keywordListParam = new KeywordListParam();
        try {
            BeanUtils.copyProperties(keywordListParam, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        // 2、 Model
        PageVo data = keywordService.list(keywordListParam);

        // 3、响应结果
        BaseRespVo baseRespVo = BaseRespVo.ok(data);
        return baseRespVo;
    }

    public BaseRespVo create(HttpServletRequest request) throws IOException {
        // 1、请求参数
        String jsonStr = request.getReader().readLine();
        Map keywordCreateVo = JacksonUtil.read(jsonStr, Map.class);

        // 2、Service处理业务
        Map data = keywordService.create(keywordCreateVo);

        // 3、响应结果
        return BaseRespVo.ok(data);
    }

    public BaseRespVo update(HttpServletRequest request) throws IOException {
        // 1、请求参数
        String jsonStr = request.getReader().readLine();
//        MarketKeyword marketKeyword = JacksonUtil.read(jsonStr, MarketKeyword.class);
        // md，响应体不同

        ObjectMapper objectMapper = null;
        {
            objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        }
        Map marketKeyword = objectMapper.readValue(jsonStr, Map.class);
//        Map updateInfoMap = JacksonUtil.read(jsonStr, Map.class);

        // 2、Service处理业务
//        MarketKeyword data = keywordService.update(marketKeyword);
        Map data = keywordService.updateMap(marketKeyword);

        // 3、响应结果
        return BaseRespVo.ok(data);
    }

    public BaseRespVo delete(HttpServletRequest request) throws IOException {
        // 1、请求参数
        String jsonStr = request.getReader().readLine();
//        MarketKeyword marketKeyword = JacksonUtil.read(jsonStr, MarketKeyword.class);
        Map deleteInfoMap = JacksonUtil.read(jsonStr, Map.class);


        // 2、Service处理业务
//        keywordService.delete(marketKeyword);
        keywordService.deleteMap(deleteInfoMap);
        
        // 3、响应结果
        return BaseRespVo.ok(null);
    }
}
